/**
 * tjftp -- A simple FTP server, using the ANSI C
 * Copyright (C) 2011  degui.chen <degui.chen@qq.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "observer.h"
#include <linux/stddef.h>
static struct Observers* _observers = NULL;

void observer_destroy() {
	struct Observers* current;
	while (_observers != NULL) {
		current = _observers;
		_observers = _observers->next;
		free(current);
	}
}

void tj_observer_register(char* key, Observer callback) {
	struct Observers* new = (struct Observers*) malloc(sizeof(struct Observers));
	strcpy(new->key, key);
	new->observer = callback;
	new->next = NULL;
	if ((_observers == NULL)) {
		_observers = new;
		return;
	}
	struct Observers* last = _observers;
	while (last->next != NULL) {
		last = last->next;
	}
	last->next = new;
}

void tj_observer_trigger(char* key, void* parm) {
	while (_observers != NULL) {
		if (!strcmp(key, _observers->key)) {
			if (NULL != _observers->observer) {
				_observers->observer(parm);
			}
		}
		_observers = _observers->next;
	}
}
